<?php
class JiesuanAction extends Action
{
    public function generateNonceStr($length=4){
        // 密码字符集，可任意添加你需要的字符
        $chars = "0123456789";
        $str = "";
        for($i = 0; $i < $length; $i++)
        {
            $str .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $str;
    }

    public function index()
    {
        dump($chars= $this->generateNonceStr());die;
        $jiesuan = array();



        if(file_exists('./Public/Conf/jiesuan.php'))

        {

            require './Public/Conf/jiesuan.php';

            $jiesuan = json_decode($jiesuan,true);

        }
//        $data = json_decode('{"code":"10000","msg":"Success","order_id":"20170228110070001502170003989995","out_biz_no":"z2017022810375199","pay_date":"2017-02-28 10:37:51"}',true);
//        $para_filter = $this->paraFilter($data);
//
//        //对待签名参数数组排序
//        $para_sort = $this->argSort($para_filter);
//        //$para_sort = sortNotifyPara($para_filter);
//
//
//        //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
//        $data = $this->createLinkstring($para_sort);
//        dump($data);die;
        import ("ORG.Alipays.AopClient");
        import ("ORG.Alipays.AlipayFundTransToaccountTransferRequest");
        //if($this->get_server_ip()===$this->get_client_ip())
        if($jiesuan['type']==2)
        {
            //执行转账功能
            $aop = new AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = $jiesuan['app_id'];//父id
            $aop->rsaPrivateKey = $jiesuan['rsaPrivateKey'];//私钥
            $aop->alipayrsaPublicKey= $jiesuan['alipayrsaPublicKey'];//公钥

            $aop->signType = 'RSA';
            $aop->postCharset='utf-8';

            $aop->format='json';
            $aop->ini();
            $request = new AlipayFundTransToaccountTransferRequest ();
            $orderno =  'z'.date('Ymdhis').round(9).round(9);//转账单号
            $payer_real_name = $jiesuan['payer_real_name'];//企业名称
            $payer_show_name = $jiesuan['payer_show_name'];//付款显示名称
            $request->setBizContent("{" .
                "    \"out_biz_no\":\"$orderno\"," .
                "    \"payee_type\":\"ALIPAY_LOGONID\"," .
                "    \"payee_account\":\"263137675@qq.com\"," .
                "    \"amount\":\"0.1\"," .
                "    \"payer_real_name\":\"$payer_real_name\"," .
                "    \"payer_show_name\":\"$payer_show_name\"," .
                "    \"payee_real_name\":\"莫伯先\"," .
                "    \"remark\":\"转账备注\"," .
                "    \"ext_param\":\"{\\\"order_title\\\":\\\"测试转账\\\"}\"" .
                "  }");
            $result = $aop->execute ( $request);
            //var_dump($result);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000){
                //out_biz_no
                //pay_date
                dump("成功") ;die;
            } else {
                dump("失败");die;
            }
        }
        $this->display();
    }
    function get_client_ip($type = 0) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下，获取客户端真实IP
            $ip=$_SERVER['HTTP_X_REAL_IP'];
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
        }else{
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }
    /*
* 获取服务器端IP地址
* @return string
*/
    function get_server_ip() {
        if (isset($_SERVER)) {
            if($_SERVER['SERVER_ADDR']) {
                $server_ip = $_SERVER['SERVER_ADDR'];
            } else {
                $server_ip = $_SERVER['LOCAL_ADDR'];
            }
        } else {
            $server_ip = getenv('SERVER_ADDR');
        }
        return $server_ip;
    }
//    function createLinkstring($para) {
//        $arg  = "";
//        while (list ($key, $val) = each ($para)) {
//            $arg.=$key."=".$val."&";
//        }
//        //去掉最后一个&字符
//        $arg = substr($arg,0,count($arg)-2);
//
//        //如果存在转义字符，那么去掉转义
//        if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
//
//        return $arg;
//    }
//    /**
//     * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串，并对字符串做urlencode编码
//     * @param $para 需要拼接的数组
//     * return 拼接完成以后的字符串
//     */
//    function createLinkstringUrlencode($para) {
//        $arg  = "";
//        while (list ($key, $val) = each ($para)) {
//            $arg.=$key."=".urlencode($val)."&";
//        }
//        //去掉最后一个&字符
//        $arg = substr($arg,0,count($arg)-2);
//
//        //如果存在转义字符，那么去掉转义
//        if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
//
//        return $arg;
//    }
//    /**
//     * 除去数组中的空值和签名参数
//     * @param $para 签名参数组
//     * return 去掉空值与签名参数后的新签名参数组
//     */
//    function paraFilter($para) {
//        $para_filter = array();
//        while (list ($key, $val) = each ($para)) {
//            if($key == "sign" || $key == "sign_type" || $val == "")continue;
//            else	$para_filter[$key] = $para[$key];
//        }
//        return $para_filter;
//    }
//    /**
//     * 对数组排序
//     * @param $para 排序前的数组
//     * return 排序后的数组
//     */
//    function argSort($para) {
//        ksort($para);
//        reset($para);
//        return $para;
//    }
}
